package com.movie.mybatis.service.impl;

import com.movie.mybatis.mapper.MovieMapper;
import com.movie.mybatis.pojo.Movie;
import com.movie.mybatis.utils.SqlSessionUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

public class MovieRootServlet extends HttpServlet {
    public void doList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        MovieMapper mapper = sqlSession.getMapper(MovieMapper.class);
        List<Movie> movies = mapper.selectAll();
        sqlSession.close();
        request.setAttribute("movieList", movies);
        request.getRequestDispatcher("/list.jsp").forward(request, response);
    }
    public void doDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String movieId = request.getParameter("movieId");
        SqlSession sqlSession = SqlSessionUtil.openSession();
        MovieMapper mapper = sqlSession.getMapper(MovieMapper.class);
        Movie movie = mapper.selectByIdtoMovieRecord(Integer.valueOf(movieId));
        sqlSession.close();
        request.setAttribute("movie", movie);
        request.getRequestDispatcher("/" + request.getParameter("f") + ".jsp").forward(request, response);
    }
    public void doModify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 解决请求体的中文乱码问题。
        request.setCharacterEncoding("UTF-8");
        // 获取表单中的数据
        String movieId = request.getParameter("movieId");
        String movieName = request.getParameter("movieName");
        String movieScore = request.getParameter("movieScore");
        SqlSession sqlSession = SqlSessionUtil.openSession();
        MovieMapper mapper = sqlSession.getMapper(MovieMapper.class);
        mapper.updateById(movieName, Double.valueOf(movieScore), Integer.valueOf(movieId));
        sqlSession.commit();
        sqlSession.close();
        response.sendRedirect(request.getContextPath() + "/root/list");
    }

    public void doDel(HttpServletRequest request, HttpServletResponse response){
        String movieId = request.getParameter("movieId");
        SqlSession sqlSession = SqlSessionUtil.openSession();
        MovieMapper mapper = sqlSession.getMapper(MovieMapper.class);
        Integer integer = mapper.deleteById(Integer.valueOf(movieId));
        System.out.println(integer);
        sqlSession.close();
    }

    public void doMovieSave(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String movieId = request.getParameter("movieId");
        String movieName = request.getParameter("movieName");
        String movieScore = request.getParameter("movieScore");

        Movie movie = new Movie(Integer.valueOf(movieId), movieName, Double.valueOf(movieScore));
        SqlSession sqlSession = SqlSessionUtil.openSession();
        MovieMapper mapper = sqlSession.getMapper(MovieMapper.class);
        Integer integer = mapper.insertMovie(movie);
        System.out.println(integer);
        sqlSession.commit();
        sqlSession.close();
        response.sendRedirect(request.getContextPath() + "/root/list");
    }

}
